<?php

namespace api\modules\v1\forms\buss;

use common\models\base\CommonStatus;
use common\models\buss\Platform;
use common\models\buss\TargetContent;
use Yii;
use yii\base\Model;


class TargetContentForm extends Model
{
    /**
     * @var int $id ID
     * @var string $title 标题
     * @var string $url 选题URL
     * @var string $account_name 账号
     * @var int $t_id 类型
     * @var string $platform_name 平台
     * @var int $like_num 点赞
     * @var int $comment_num 评论
     * @var int $collect_num 收藏
     * @var int $forward_num 转发
     * @var int $c_id 分类
     * @var int $topic 话题
     */
    public $id;
    public $title;
    public $url;
    public $account_name;
    public $t_id;
    public $platform_name;
    public $like_num;
    public $comment_num;
    public $collect_num;
    public $forward_num;
    public $c_id;
    public $topic;

    /**
     * @var TargetContent $_targetContent
     * @var CommonStatus $_tCommonStatus
     * @var CommonStatus $_cCommonStatus
     */
    protected $_targetContent;
    protected $_tCommonStatus;
    protected $_cCommonStatus;


    public function rules()
    {
        return [
            // 参数验证
            [['title', 'account_name'], 'required', 'message' => '{attribute}不能为空', 'on' => ['create']],
            [['id', 'title', 'account_name'], 'required', 'message' => '{attribute}不能为空', 'on' => ['edit']],
            [['id'], 'required', 'message' => '{attribute}不能为空', 'on' => ['delete']],

            [['id'], 'checkId', 'skipOnEmpty' => false, 'on' => ['edit', 'delete']],
            [['title'], 'checkTitle', 'skipOnEmpty' => false, 'on' => ['create', 'edit']],
            [['url'], 'checkUrl', 'skipOnEmpty' => true, 'on' => ['create', 'edit']],
            [['account_name'], 'checkAccountName', 'skipOnEmpty' => true, 'on' => ['create', 'edit']],
            [['t_id'], 'checkTid', 'skipOnEmpty' => true, 'on' => ['create', 'edit']],
            [['platform_name'], 'checkPlatformName', 'skipOnEmpty' => true, 'on' => ['create', 'edit']],
            [['like_num'], 'checkLikeNum', 'skipOnEmpty' => false, 'on' => ['create', 'edit']],
            [['collect_num'], 'checkCollectNum', 'skipOnEmpty' => false, 'on' => ['create', 'edit']],
            [['comment_num'], 'checkCommentNum', 'skipOnEmpty' => false, 'on' => ['create', 'edit']],
            [['forward_num'], 'checkForwardNum', 'skipOnEmpty' => false, 'on' => ['create', 'edit']],
            [['c_id'], 'checkCid', 'skipOnEmpty' => true, 'on' => ['create', 'edit']],
            [['topic'], 'checkTopic', 'skipOnEmpty' => true, 'on' => ['create', 'edit']],
        ];
    }

    public function scenarios()
    {
        $scenarios = parent::scenarios();

        // 新增
        $scenarios['create'] = ['title', 'url', 'account_name', 't_id', 'platform_name', 'like_num', 'comment_num', 'collect_num', 'forward_num', 'c_id', 'topic'];

        // 编辑
        $scenarios['edit'] = ['id', 'title', 'url', 'account_name', 't_id', 'platform_name', 'like_num', 'comment_num', 'collect_num', 'forward_num', 'c_id', 'topic'];

        // 删除
        $scenarios['delete'] = ['id'];

        return $scenarios;
    }

    /**
     * @return array
     */
    public function attributeLabels()
    {
        return [
            'id' => 'ID',
            'project_id' => '项目ID',
            'type' => '类型',
            'title' => '标题',
            'content' => '选题',
            'c_id' => '分类',
            'keyword' => '关键词',
            'tags' => '标签',
            'is_stream' => '投流',
            'publish_status' => '状态',
            'attachment' => '附件',
        ];
    }

    /**
     * 检验选题ID
     * @param $attribute
     * @return void
     */
    public function checkId($attribute): void
    {
        if ($this->hasErrors()) {
            return;
        }

        $content = $this->getTargetContent();
        if (!$content) {
            $this->addError($attribute, '选题不存在');
            return;
        }

        if ($content->status == TargetContent::STATUS_DELETE) {
            $this->addError($attribute, '选题已删除');
            return;
        }
    }

    /**
     * 校验标题
     * @param $attribute
     * @return void
     */
    public function checkTitle($attribute): void
    {
        if ($this->hasErrors()) {
            return;
        }
        $len = mb_strlen($this->title);
        if ($len > 64) {
            $this->addError($attribute, '标题过长，最多64个字');
            return;
        }
    }

    /**
     * 校验url
     * @param $attribute
     * @return void
     */
    public function checkUrl($attribute): void
    {
        if ($this->hasErrors()) {
            return;
        }
        $len = mb_strlen($this->url);
        if ($len > 256) {
            $this->addError($attribute, 'url过长，最多256个字节');
            return;
        }
    }

    /**
     * 校验账号
     * @param $attribute
     * @return void
     */
    public function checkAccountName($attribute): void
    {
        if ($this->hasErrors()) {
            return;
        }
        $len = mb_strlen($this->account_name);
        if ($len > 32) {
            $this->addError($attribute, '账号过长，最多32个字节');
            return;
        }
    }

    /**
     * 校验账号类型
     * @param $attribute
     * @return void
     */
    public function checkTid($attribute): void
    {
        if ($this->hasErrors()) {
            return;
        }
        if (empty($this->t_id)) {
            return;
        }
        $tCommonStatus = $this->getTCommonStatus();
        if (empty($tCommonStatus)) {
            $this->addError($attribute, '类型不存在');
            return;
        }
        if ($tCommonStatus->status == CommonStatus::STATUS_DELETE) {
            $this->addError($attribute, '类型已删除');
            return;
        }
        if ($tCommonStatus->type != CommonStatus::TYPE_ACCOUNTS) {
            $this->addError($attribute, '账号类型错误，非账号类型');
            return;
        }
        if ($this->getScenario() == 'edit') {
            $targetContent = $this->getTargetContent();
            if ($targetContent->t_id == $this->t_id) {
                return;
            }
        }
        if ($tCommonStatus->status == CommonStatus::STATUS_INVALID) {
            $this->addError($attribute, '类型已禁用');
            return;
        }
    }

    /**
     * 校验平台
     * @param $attribute
     * @return void
     */
    public function checkPlatformName($attribute): void
    {
        if ($this->hasErrors()) {
            return;
        }
        if (empty($this->platform_name)) {
            return;
        }
        $len = mb_strlen($this->platform_name);
        if ($len > 32) {
            $this->addError($attribute, '平台名称过长，最多32字节');
            return;
        }
    }

    /**
     * 校验点赞量
     * @param $attribute
     * @return void
     */
    public function checkLikeNum($attribute): void
    {
        if ($this->hasErrors()) {
            return;
        }
        if (empty($this->like_num) && is_numeric($this->like_num) === false) {
            return;
        }
        if ($this->like_num < 0) {
            $this->addError($attribute, '点赞数不能为负数');
            return;
        }
    }

    /**
     * 校验评论数
     * @param $attribute
     * @return void
     */
    public function checkCommentNum($attribute): void
    {
        if ($this->hasErrors()) {
            return;
        }
        if (empty($this->comment_num) && is_numeric($this->comment_num) === false) {
            return;
        }
        if ($this->comment_num < 0) {
            $this->addError($attribute, '评论数不能为负数');
            return;
        }
    }

    /**
     * 校验收藏数
     * @param $attribute
     * @return void
     */
    public function checkCollectNum($attribute): void
    {
        if ($this->hasErrors()) {
            return;
        }
        if (empty($this->collect_num) && is_numeric($this->collect_num) === false) {
            return;
        }
        if ($this->collect_num < 0) {
            $this->addError($attribute, '收藏数不能为负数');
            return;
        }
    }

    /**
     * 校验转发数
     * @param $attribute
     * @return void
     */
    public function checkForwardNum($attribute): void
    {
        if ($this->hasErrors()) {
            return;
        }
        if (empty($this->forward_num) && is_numeric($this->forward_num) === false) {
            return;
        }
        if ($this->forward_num < 0) {
            $this->addError($attribute, '转发数不能为负数');
            return;
        }
    }

    /**
     * 校验分类
     * @param $attribute
     * @return void
     */
    public function checkCid($attribute): void
    {
        if ($this->hasErrors()) {
            return;
        }
        if (empty($this->c_id)) {
            return;
        }
        $cCommonStatus = $this->getCCommonStatus();
        if (empty($cCommonStatus)) {
            $this->addError($attribute, '分类不存在');
            return;
        }
        if ($cCommonStatus->status == CommonStatus::STATUS_DELETE) {
            $this->addError($attribute, '分类已删除');
            return;
        }
        if ($cCommonStatus->type != CommonStatus::TYPE_CONTENT) {
            $this->addError($attribute, '类型错误，非分类类型');
            return;
        }
        if ($this->getScenario() == 'edit') {
            $targetContent = $this->getTargetContent();
            if ($targetContent->c_id == $this->c_id) {
                return;
            }
        }
        if ($cCommonStatus->status == CommonStatus::STATUS_INVALID) {
            $this->addError($attribute, '分类已禁用');
            return;
        }
    }

    /**
     * 校验话题
     * @param $attribute
     * @return void
     */
    public function checkTopic($attribute): void
    {
        if ($this->hasErrors()) {
            return;
        }
        $len = mb_strlen($this->topic);
        if ($len > 128) {
            $this->addError($attribute, '话题过长，最多128个字节');
            return;
        }
    }

    /**
     * 获取选题信息
     * @return TargetContent|null
     */
    public function getTargetContent(): ?TargetContent
    {
        if (empty($this->_targetContent)) {
            $this->_targetContent = TargetContent::findOne($this->id);
        }

        return $this->_targetContent;
    }

    /**
     * 获取账号类型
     * @return CommonStatus|null
     */
    public function getTCommonStatus(): ?CommonStatus
    {
        if (empty($this->_tCommonStatus)) {
            $this->_tCommonStatus = CommonStatus::findOne($this->t_id);
        }

        return $this->_tCommonStatus;
    }

    /**
     * 获取分类
     * @return CommonStatus|null
     */
    public function getCCommonStatus(): ?CommonStatus
    {
        if (empty($this->_cCommonStatus)) {
            $this->_cCommonStatus = CommonStatus::findOne($this->c_id);
        }

        return $this->_cCommonStatus;
    }
}
